<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>

						<el-col :span="6">
							<el-form-item label="坐席名称" prop="name">
								<el-input v-model="tableData.param.name" placeholder="请输入坐席名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item label="企业名称" prop="companyCode">
								<el-select v-model="tableData.param.companyCode" placeholder="请选择企业名称" clearable
									filterable style="width: 100%;">
									<el-option v-for="item in tableData.companyData" :value="item.value"
										:label="item.label" :key="item.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" @click="handleAdd"
									v-if="permissionStore.isPermissionsBtn('012005001')"><el-icon>
										<Plus />
									</el-icon>新增</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="名称" align="center" prop="name" min-width="100px" show-overflow-tooltip />
				<el-table-column label="工号" align="center" prop="staffId" min-width="100px" show-overflow-tooltip />
				<el-table-column label="坐席等级" align="center" prop="seatLevel" width="100px" />
				<el-table-column label="话机类型" align="center" min-width="100px">
					<template #default="{ row }">{{ dictionary[row.phoneType] }}</template>
				</el-table-column>
				<el-table-column label="字段话后" align="center" min-width="100px">
					<template #default="{ row }">{{ dictionary[row.autoAfterCall] }}</template>
				</el-table-column>
				<el-table-column label="外呼线路" align="center" min-width="150px" show-overflow-tooltip>
					<template #default="{ row }">{{ disposeLine(row.lines) }}</template>
				</el-table-column>
				<el-table-column label="企业名称" align="center" min-width="200px" show-overflow-tooltip>
					<template #default="{ row }">{{ dictionary.companyObj[row.companyCode] }}</template>
				</el-table-column>
				<el-table-column label="分机号" align="center" prop="extNum" min-width="100px" show-overflow-tooltip />
				<el-table-column label="坐席组" align="center" min-width="150px" prop="seatGroupNames"
					show-overflow-tooltip />
				<el-table-column label="操作" align="center" min-width="200px" fixed="right"
					v-if="permissionStore.isPermissionsBtn(['012005002', '012005003'])">
					<template #default="scope">
						<el-button type="primary" link @click="handleEdit(scope.row)"
							v-if="permissionStore.isPermissionsBtn('012005002')"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
						<el-button type="danger" link @click="handleDelete(scope.row)"
							v-if="permissionStore.isPermissionsBtn('012005003')"><el-icon>
								<Delete />
							</el-icon>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<addView ref="addRef" :companyData="tableData.companyData" :phoneTypeList="dictionary.phoneTypeList"
			:outcallLineData="tableData.outcallLineData" :autoAfterCallList="dictionary.autoAfterCallList"
			@getList="getList">
		</addView>
		<editView ref="editRef" :companyData="tableData.companyData" :phoneTypeList="dictionary.phoneTypeList"
			:outcallLineData="tableData.outcallLineData" :autoAfterCallList="dictionary.autoAfterCallList"
			@getList="getList">
		</editView>
	</div>
</template>
<script lang="ts">
import { outcallAccountListApi, delOutcallAccountApi } from '@/api/operationsManage/outcallAccount';
import { companySelectApi } from '@/api/numberManage/allocationRecords';
import { selectOutcallLineApi } from '@/api/operationsManage/outboundLine';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission";
import addView from './components/add.vue'
import editView from './components/edit.vue'
export default defineComponent({
	name: 'outcallAccountList',		//外呼坐席账户管理
	components: { addView, editView },
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const companyAllotViewRef = ref();
		const permissionStore = usePermissionStore()
		const state = reactive({
			tableData: {
				data: [],
				companyData: [],	//企业
				outcallLineData: [],	//线路
				total: 0,
				loading: false,
				param: {
					pageNumber: 1,
					pageLength: 10,
					companyCode: undefined,
					name: undefined,
				},
			},
			dictionary: {
				'FIXED_PHONE': '固定话机',
				'SOFT_PHONE': '软电话',
				'WEB_PHONE': 'web电话',
				'CLOSE': '关闭',
				'INBOUND_ONLY': '仅呼入',
				'OUTBOUND_ONLY': '仅呼出',
				'ALL': '全部',
				phoneTypeList: [
					{
						value: 'FIXED_PHONE',
						label: '固定话机'
					},
					{
						value: 'SOFT_PHONE',
						label: '软电话'
					}, {
						value: 'WEB_PHONE',
						label: 'web电话'
					}
				],
				autoAfterCallList: [
					{
						value: 'CLOSE',
						label: '关闭'
					}, {
						value: 'INBOUND_ONLY',
						label: '仅呼入'
					}, {
						value: 'OUTBOUND_ONLY',
						label: '仅呼出'
					}, {
						value: 'ALL',
						label: '全部'
					},
				],
				companyObj: {},
				outcallLineObj: {}
			}
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
			getCompanyList();
			getOutcallLineList()
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			outcallAccountListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};

		// 新增
		const handleAdd = () => {
			addRef.value.openDialog()
		}
		// 修改
		const handleEdit = (row) => {
			const rawRow = structuredClone(toRaw(row));
			editRef.value.openDialog(rawRow);
		}

		// 删除
		const handleDelete = (row: any) => {
			let msg = `此操作将永久删除数据：[${row.name}]，是否继续?`;
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delOutcallAccountApi({ id: row.id }).then(() => {
						ElMessage.success('删除成功');
						getList();
					})
				})
				.catch(() => { });
		};
		// 获取企业下拉
		const getCompanyList = () => {
			companySelectApi().then((res: any) => {
				let data = res.data
				state.dictionary.companyObj = data
				for (let key in data) {
					let obj = {
						value: key,
						label: data[key]
					}
					state.tableData.companyData.push(obj)
				}
			})
		}

		// 线路下拉
		const getOutcallLineList = () => {
			selectOutcallLineApi().then((res: any) => {
				let data = res.data
				state.dictionary.outcallLineObj = data
				for (let key in data) {
					let obj = {
						value: key,
						label: data[key]
					}
					state.tableData.outcallLineData.push(obj)
				}
			})
		}

		// 处理线路数据
		const disposeLine = (obj) => {
			let str = ''
			for (let key in obj) {
				if (str) str += '、' + state.dictionary.outcallLineObj[obj[key]]
				else str = state.dictionary.outcallLineObj[obj[key]]
			}
			return str
		}
		return {
			proxy,
			loading,
			queryRef,
			addRef,
			editRef,
			companyAllotViewRef,
			permissionStore,
			resetQuery,
			getList,
			handleAdd,
			handleEdit,
			handleDelete,
			disposeLine,
			...toRefs(state),
		};
	},
});
</script>
